//
//  YRCompose.swift
//  新浪微博
//
//  Created by Viper on 16/7/3.
//  Copyright © 2016年 Viper. All rights reserved.
//

import UIKit

class YRComposeTextView: UITextView {
    //重写父类已有属性
    override var font: UIFont?{
        //通过didSet监听font改变
        didSet{
           
            placeholderLabel.font = font
            
        }
    }
    
    //占位文字
    var placeholder : String?{
        
        didSet{
            
            placeholderLabel.text = placeholder
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - 设置视图
    
    private func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        
        addSubview(placeholderLabel)
        
        placeholderLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(self).offset(8)
            make.leading.equalTo(self).offset(5)
            make.width.equalTo(self).offset(SCREENW - 10)
        }
                // 通过系统通知监听textView 文字改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YRComposeTextView.textViewTextDidChange), name: UITextViewTextDidChangeNotification, object: nil)
    }
    //移除通知
    deinit{
        
         NSNotificationCenter.defaultCenter().removeObserver(self)
        
    }
    //监听方法
    func textViewTextDidChange(){
        placeholderLabel.hidden = self.hasText()
    }
    
    
   //MARK: - 懒加载控件
    private lazy var placeholderLabel : UILabel = {
        
        let label = UILabel()
        label.textColor = UIColor.lightGrayColor()
        label.numberOfLines = 0
        
        return label
    }()
    
   

}
